﻿using BookModel;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    //书架信息表
   
   public class ReadingInfoDAL
   {
       #region 查询所有书架
       public List<ReadingInfo> GetReading()
       {
           string sql = "select*from ReadingInfo";
           List<ReadingInfo> lt = new List<ReadingInfo>();
           if (SQLHelp.OpenConn())
           {
               SqlDataReader sdr = SQLHelp.ExecReader(sql);
               if (sdr != null)
               {
                   while (sdr.Read())
                   {
                       lt.Add(new ReadingInfo
                       {
                           RID = sdr[0] as string,
                           RName = sdr[1] as string,
                           Raddress = sdr[2] as string,
                           Remark = sdr["Remark"] as string
                       });
                   }
               }
               SQLHelp.CloseConn();
           }
           return lt;
       }
       #endregion
       public string AddReading(ReadingInfo re)//添加
       {
           string sql = "insert into ReadingInfo values(@RID,@RName,@Raddress,@Remark)";
           SqlParameter[] sp = new SqlParameter[4];
           sp[0] = new SqlParameter("@RID", re.RID);
           sp[1] = new SqlParameter("@RName", re.RName);
           sp[2] = new SqlParameter("@Raddress", re.Raddress);
           sp[3] = new SqlParameter("@Remark", re.Remark);
           int count = SQLHelp.ExecQuery(sql, System.Data.CommandType.Text, sp);
           switch (count)
           {
               case -2:
                   sql= "数据库连接出错，请联系管理员";
                   break;
               case -1:
                   sql= "数据输入出错，请检查输入的数据";
                   break;
               case 0:
                   sql= "无任何添加";
                   break;
               default:
                   sql = "成功新增"+count+"座书架信息";
                   break;
           }

           return sql;
       }
       public string UPdatered(ReadingInfo re)//修改
       {
           string sql = "update ReadingInfo set RName=@RName,Raddress=@Raddress,Remark=@Remark where RID=@RID";
           SqlParameter[] sp = new SqlParameter[4];
           sp[0] = new SqlParameter("@RID", re.RID);
           sp[1] = new SqlParameter("@RName", re.RName);
           sp[2] = new SqlParameter("@Raddress", re.Raddress);
           sp[3] = new SqlParameter("@Remark", re.Remark);
           int count = SQLHelp.ExecQuery(sql, System.Data.CommandType.Text, sp);
           switch (count)
           {
               case -2:
                   sql= "数据库连接出错，请联系管理员";
                   break;
               case -1:
                   sql= "数据输入出错，请检查输入的数据";
                   break;
               case 0:
                   sql= "无任何添加";
                   break;
               default:
                   sql = "修改成功"+count+"座书架信息";
                   break;

           }

           return sql;
       }
   }
}
